package com.hyk.commonLib.common.utils;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.annotation.BoolRes;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DimenRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;

import com.hyk.commonLib.R;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * Created by 贺玉琨 on 2019/3/6 0006.
 */
public class ResUtils {
    public static Resources getResource() {
        return AppUtils.getAppContext().getResources();
    }

    public static float getDensity() {
        return AppUtils.getAppContext().getResources().getDisplayMetrics().density;
    }

    public static Drawable getDrawable(@DrawableRes int drawableRes) {
        if (drawableRes == 0) {
            return null;
        }
        return ContextCompat.getDrawable(AppUtils.getAppContext(), drawableRes);
    }

    @ColorInt
    public static int getColor(@ColorRes int colorRes) {
        return ContextCompat.getColor(AppUtils.getAppContext(), colorRes);
    }

    @ColorInt
    public static int getColor(@ColorRes int colorRes, View v) {
        return ContextCompat.getColor(v.getContext(), colorRes);
    }

    public static float getDimension(@DimenRes int dimenID) {
        return getResource().getDimension(dimenID);
    }

    public static String getString(@StringRes int stringId) {
        return getResource().getString(stringId);
    }

    public static boolean getBoolean(@BoolRes int id) {
        return getResource().getBoolean(id);
    }

    @Nullable
    public static Bitmap getBitmap(@DrawableRes int drawableRes) {
        return BitmapFactory.decodeResource(getResource(), drawableRes);
//        return ImageUtils.drawableToBitmap(getDrawable(drawableRes));
    }

    @Nullable
    public static Bitmap getBitmap(@DrawableRes int drawableRes, int maxWidth, int maxHeight) {
        return ImageUtils.drawableToBitmap(getDrawable(drawableRes), maxWidth, maxHeight);
    }

    /**
     * 读取assets下的txt文件，返回utf-8 String
     *
     * @param fileName
     * @return
     */
    public static String readAssetsTxt(String fileName) {
        try {
            //Return an AssetManager instance for your application's package
            InputStream is = getResource().getAssets().open(fileName);
            int size = is.available();
            // Read the entire asset into a local byte buffer.
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            // Convert the buffer into a string.
            String text = new String(buffer, StandardCharsets.UTF_8);
            // Finally stick the string into the text view.
            return text;
        } catch (IOException e) {
            // Should never happen!
//            throw new RuntimeException(e);
            e.printStackTrace();
        }
        return "";
    }

    public static class Color {
        @ColorInt
        public static final int colorPrimary = getColor(R.color.colorPrimary);
        @ColorInt
        public static final int colorAccent = getColor(R.color.colorAccent);
        @ColorInt
        public static final int textColorPrimary = getColor(R.color.text_color_primary);
        @ColorInt
        public static final int textColorSecondary = getColor(R.color.text_color_secondary);
        @ColorInt
        public static final int textColorAccent = getColor(R.color.text_color_accent);
    }

    public static class Dimension {
        public static final int textSizeXXXXXX = (int) getDimension(R.dimen.text_size_xxxxxx);
        public static final int textSizeXXXXX = (int) getDimension(R.dimen.text_size_xxxxx);
        public static final int textSizeXXXX = (int) getDimension(R.dimen.text_size_xxxx);
        public static final int textSizeXXX = (int) getDimension(R.dimen.text_size_xxx);
        public static final int textSizeXX = (int) getDimension(R.dimen.text_size_xx);
        public static final int textSizeX = (int) getDimension(R.dimen.text_size_x);
        public static final int textSizeM = (int) getDimension(R.dimen.text_size_m);
        public static final int textSizeS = (int) getDimension(R.dimen.text_size_s);
    }
}
